package com.wxc.api.admin.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import com.wxc.api.admin.entity.SysMenu;
import com.wxc.api.admin.entity.SysRole;
import com.wxc.api.admin.entity.SysUserRoleRelation;
import com.wxc.api.admin.service.ISysMenuService;
import com.wxc.api.admin.service.ISysRoleMenuRelationService;
import com.wxc.api.admin.service.ISysRoleService;
import com.wxc.api.admin.service.ISysUserRoleRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import cn.dev33.satoken.stp.StpInterface;


/**
 * 自定义权限验证接口扩展
 */
@Component    // 保证此类被SpringBoot扫描，完成Sa-Token的自定义权限验证扩展
public class StpInterfaceImpl implements StpInterface {

    @Autowired
    private ISysUserRoleRelationService sysUserRoleRelationService;

    @Autowired
    private ISysRoleMenuRelationService sysRoleMenuRelationService;

    @Autowired
    private ISysMenuService sysMenuService;

    @Autowired
    private ISysRoleService sysRoleService;

    /**
     * 返回一个账号所拥有的权限码集合
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {

        SysUserRoleRelation userRoleRelation = sysUserRoleRelationService.getInfo(Integer.valueOf(loginId.toString()));

        List<Integer> ids = sysRoleMenuRelationService.getInfo(userRoleRelation.getRoleId());

        List<SysMenu> menu = sysMenuService.getInfo(ids);

        System.out.println(menu);

        List<String> list = new ArrayList<String>();

        menu.forEach(value -> {
            String permission = value.getPermission();
            if(!Objects.equals(permission, "")){
                list.add(permission);
            }
        });

        System.out.println("权限：");
        System.out.println(list);
        System.out.println(ids);
        return list;
    }

    /**
     * 返回一个账号所拥有的角色标识集合 (权限与角色可分开校验)
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {

        SysUserRoleRelation ur = sysUserRoleRelationService.getInfo(Integer.valueOf(loginId.toString()));

        SysRole role = sysRoleService.getInfo(ur.getRoleId());

        List<String> list = new ArrayList<String>();

        list.add(role.getRoleCode());

        System.out.println("角色：");
        System.out.println(list);
        return list;
    }

}